package com.example.lenovo.guide.mode;

import com.example.lenovo.guide.Iface.WeatherInface;
import com.example.lenovo.guide.Listener.WeatherListener;
import com.example.lenovo.guide.Service.WeatherService;
import com.example.lenovo.guide.bean.WeatherResult;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Lenovo on 2017/3/14.
 */

public class WeatherMode implements WeatherInface{
private  static  final  String BASE_URL="http://www.weather.com.cn/data/";
    private WeatherService service;
    private Retrofit retrofit;

    @Override
    public void getWeather(String cityNumber, final WeatherListener Listener) {
//步骤1
        retrofit=new Retrofit.Builder()
        .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //步骤2 创建API请求
        service=retrofit.create(WeatherService.class);
        Call<WeatherResult> call=service.getResult(cityNumber);


        //步骤3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
          if (response.isSuccessful() && response.body()!=null)
          {
              Listener.onResponse(response.body().getWeatherinfo());
          }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
             Listener.onFailure(t.toString());
            }
        });



    }
}
